package com.tanhua.service.impl;

import com.tanhua.api.houtai.SoulApi;
import com.tanhua.api.houtai.SoulQuestionApi;
import com.tanhua.domain.mongo.Soul;
import com.tanhua.service.SoulService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author user
 */
@Service
public class SoulServiceImpl implements SoulService {

    @Reference
    public SoulApi soulApi;

    @Reference
    public SoulQuestionApi soulQuestionApi;

    /**
     * 查询视频列表
     */
    @Override
    public ResponseEntity getSoulList() {
        List<Soul> soulList = soulApi.getSoulList();
        return ResponseEntity.ok(soulList);
    }

    /**
     * 根据id删除试卷
     *
     * @param soulId
     * @return
     */
    @Override
    public ResponseEntity deleteSoulId(Integer soulId) {
//        先根据id查询查要删除的考试表
        Soul soul = soulApi.findSoulId(soulId);
        //        然后根据里的id遍历删除题和选项
        if (soul.getQuestionIds() != null) {
            for (String questionId : soul.getQuestionIds()) {
                soulQuestionApi.deleteQuestionById(Integer.parseInt(questionId));
            }
        }
//        然后删除soul表
        soulApi.deleteSoulId(soulId);
        return ResponseEntity.ok(null);
    }

    /**
     * 添加试卷
     *
     * @param soul
     * @return
     */
    @Override
    public ResponseEntity addSoul(Soul soul) {
        soul.setCover("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/tanhua/avatar_8.jpg");
        soulApi.addSoul(soul);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改试卷
     *
     * @param soul
     * @return
     */
    @Override
    public ResponseEntity updateSoul(Soul soul) {
        soulApi.updateSoul(soul);
        return ResponseEntity.ok(null);
    }

    /**
     * 根据id查询单个试卷
     *
     * @param souid
     * @return
     */
    @Override
    public ResponseEntity getSoulId(String souid) {
        Soul soul = soulApi.findSoulId(Integer.parseInt(souid));
        return ResponseEntity.ok(soul);
    }
}
